﻿using System;
using System.Linq;

namespace ALDIOrganizer
{
    internal class Qualifier
    {        
        internal delegate bool delQualifier(Cell pCell);

        internal bool QAlwaysTrue(Cell pCell)
        {
            return true;
        }

        internal bool QFirstColumn(Cell pCell)
        {
            return pCell.GetColumn == 0;
        }
        
        internal bool QTemplate(Cell pCell)
        {
            return pCell.GetParentMode == SheetType.Template;
        }

        internal bool QRotaAndEditable(Cell pCell)
        {
            if (pCell.GetParentMode == SheetType.Rota &
                pCell.IsEditable)
            {
                return Program.Logged.CanEditRota(pCell.GetParentAsRota.Monday);
            }
            return false;
        }

        internal bool QAnyShifts(Cell pCell)
        {
            return pCell.GetParent.GetAllShifts.ToArray().Length > 0;
        }

        internal bool QRotaAndShift(Cell pCell)
        {
            return (pCell.GetParentMode == SheetType.Rota) &&
                (pCell.MyCellType == CellType.Shift);
        }

        internal bool QShift(Cell pCell)
        {
            return pCell.MyCellType == CellType.Shift;
        }

        internal bool QEditable(Cell pCell)
        {
            if (pCell.GetParent.IsTemplate)
            {
                return pCell.IsEditable;
            }
            return pCell.IsEditable &&
                   Program.Logged.CanEditRota(pCell.GetParentAsRota.Monday);
        }

        internal bool QSick(Cell pCell)
        {            
            if (pCell.MyCellType != CellType.Holiday)
            {
                return false;
            }
            if (new Holiday(
                SQLiteAccess.GetHolidays(pCell.GetStatusID[0])).type !=
                HolidayType.sick)
            {
                return false;
            }

            return pCell.GetMyDate < DateTime.Now ?
                Program.Logged.CanEditPastHolidays :
                Program.Logged.CanEditHolidays;
        }

        internal bool QAnyShiftsInRow(Cell pCell)
        {
            var canEdit = (pCell.GetParentMode == SheetType.Rota) ?
                Program.Logged.CanEditRota(pCell.GetParentAsRota.Monday) :
                Program.Logged.CanEditTemplates;
            return canEdit && pCell.HasAnyShiftsInItsRow();
        }

        internal bool QHoliday(Cell pCell)
        {
            if (pCell.MyCellType == CellType.Holiday)
            {
                return (pCell.GetMyDate < DateTime.Now ?
                    Program.Logged.CanEditPastHolidays :
                    Program.Logged.CanEditHolidays) &&
                    !QSick(pCell);
            }
            return false;
        }

        internal bool QRequest(Cell pCell)
        {
            return pCell.HasAnyRequests &&
                Program.Logged.CanEditRequests;
        }

        internal bool QEmployee(Cell pCell)
        {
            return pCell.MyCellType == CellType.Employee;
        }

        internal bool QEmployeeHoliday(Cell pCell)
        {
            // another option :)
            return new [] 
            {
                CellType.Employee,
                CellType.Holiday 
            }.Contains(pCell.MyCellType);
        }
    }
}
